﻿using MergeTool.ClassLib.Configuration;

namespace MergeTool.UI.ViewModels
{
    public class MainViewModel : MyCustomViewModelBase
    {
        private const string EplicitChangesetTextboxInitialText = "Type whitespace separated changeset ids here.";

        public MainViewModel()
        {
            _settings = Settings.GetInstance();
            IsMergeByChangesets = _settings.IsMergeByChangesets;
            MergeLoggedUserChangesets = _settings.MergeLoggedUserChangesets;
            IsKeepOriginalOwnerOfMergedChangeset = _settings.IsKeepOriginalOwnerOfMergedChangeset;
            ExplicitChangesetArray = EplicitChangesetTextboxInitialText;
        }

        private readonly Settings _settings;

        public static string StaticExplicitChangesetArray { get; set; }

        public static bool StaticIsMergeMultipleSpecifiedChangesets { get; set; }

        public string ExplicitChangesetArray
        {
            get { return _explicitChangesetArray; }
            set
            {
                SetProperty(ref this._explicitChangesetArray, value);
                StaticExplicitChangesetArray = value;
            }
        }

        private string _explicitChangesetArray;

        public bool IsMergeMultipleSpecifiedChangesets
        {
            get { return _isMergeMultipleSpecifiedChangesets; }
            set
            {
                SetProperty(ref this._isMergeMultipleSpecifiedChangesets, value);
                StaticIsMergeMultipleSpecifiedChangesets = value;
                if (value && ExplicitChangesetArray.Equals(EplicitChangesetTextboxInitialText))
                {
                    ExplicitChangesetArray = "";
                }
            }
        }

        private bool _isMergeMultipleSpecifiedChangesets;

        private bool _mergeLoggedUserChangesets;

        public bool MergeLoggedUserChangesets
        {
            get { return this._mergeLoggedUserChangesets; }
            set
            {
                SetProperty(ref _mergeLoggedUserChangesets, value);
                _settings.MergeLoggedUserChangesets = value;

                if (value && IsMergeByChangesets == false)
                {
                    IsMergeByChangesets = true;
                }
            }
        }

        private bool _isMergeByChangesets;

        public bool IsMergeByChangesets
        {
            get { return this._isMergeByChangesets; }
            set
            {
                SetProperty(ref _isMergeByChangesets, value);
                _settings.IsMergeByChangesets = value;

                if (!value && MergeLoggedUserChangesets)
                {
                    MergeLoggedUserChangesets = false;
                }
            }
        }

        private bool _isKeepOriginalOwnerOfMergedChangeset;

        public bool IsKeepOriginalOwnerOfMergedChangeset
        {
            get { return this._isKeepOriginalOwnerOfMergedChangeset; }
            set
            {
                SetProperty(ref _isKeepOriginalOwnerOfMergedChangeset, value);
                _settings.IsKeepOriginalOwnerOfMergedChangeset = value;
            }
        }
    }
}
